package todlu.RssReader.QQWeibo;

import com.tencent.weibo.api.T_API;
import com.tencent.weibo.api.User_API;
import com.tencent.weibo.beans.OAuth;
import com.tencent.weibo.utils.OAuthClient;
import com.tencent.weibo.beans.Account;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;


public class QWeibo {
	public static final String CONSUMER_KEY = "801002037";		//app_key
	public static final String CONSUMER_SECRET = "206b2c52db514d06658307ce514d39e7";	//app secret
	public static final String AUTHORIZE_URL = "https://open.t.qq.com/cgi-bin/authorize";
	
	public final static int REQUEST_CODE=1;
	
	private static OAuth oauth = new OAuth(CONSUMER_KEY, CONSUMER_SECRET, "null");
	private static OAuthClient authClient = new OAuthClient();
	private static Account account = null;
	private static boolean isBound = false;
	
	public static boolean checkBindState(Activity activity)
	{
		oauth = new OAuth(CONSUMER_KEY, CONSUMER_SECRET, "null");
		String[] oauth_token_array = TokenStore.fetch(activity);
		String oauth_token = oauth_token_array[0];
		String oauth_token_secret = oauth_token_array[1];
		 
		if(oauth_token != null && oauth_token_secret != null) // 已经有access token
		{ 
			oauth = new OAuth(CONSUMER_KEY, CONSUMER_SECRET, "null");
			oauth.setOauth_token(oauth_token);
			oauth.setOauth_token_secret(oauth_token_secret);
			
			isBound = true;
			
			return true;
		}
		else
		{
			isBound = false;
			return false;
		}
	}
	
	public static String getMyInfo()
	{
		if(!isBound)
		{
			return null;
		}
		
		User_API userapi = new User_API();
		try {
			return userapi.info(oauth, "json");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static String getAuthorizeUrl()
	{
		try 
		{
			oauth = new OAuth(CONSUMER_KEY, CONSUMER_SECRET, "null");
			oauth = authClient.requestToken(oauth);
			if (oauth.getStatus() == 1) 
			{
				System.out.println("Get Request Token failed!");
				return null;
			}
			else 
			{
				String oauth_token = oauth.getOauth_token();
				
				String url = "http://open.t.qq.com/cgi-bin/authorize?oauth_token="
					+ oauth_token;
				
				//Log.d("RSSReader-QWeibo", "AndroidExample url = "+url);

				return url;
			}
		} 
		catch (Exception e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public static boolean verify(Activity activity, String verifyCode)
	{
		try
		{
			oauth.setOauth_verifier(verifyCode);
			
			oauth = authClient.accessToken(oauth);
			
			if (oauth.getStatus() == 2) 
			{
				isBound = false;
				return false;
			} 
			else 
			{	
				TokenStore.store(activity, oauth);		//保存
				isBound = true;
				return true;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			isBound = false;
			return false;
		}
	}
	
	public static Account getAccount()
	{
		if(oauth == null)
		{
			return null;
		}
		
		account = oauth.getAccount();
		
		return account;
	}
	
	public static boolean broadcast(String content)
	{
		if(isBound)
		{
			T_API tapi = new T_API();
			try {
				tapi.add(oauth, "json", content, "127.0.0.1", "", "");
				return true;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		
		return false;
	}
	
	public static boolean broadcastWithImage(String content, String imgPath)
	{
		if(isBound)
		{
			T_API tapi = new T_API();
			try {
				tapi.add_pic(oauth, "json", content, "127.0.0.1", imgPath);
				return true;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		
		return false;
	}
}
